import json
import requests

# 交易网站的网址
def get_multichain_data():
    # 设置最终存放清洗过的数据列表
    data = []

    # 设置链名存放数据列表
    chain_name=[]
    # 读取chainid数据
    f = open('../data/chainID_data.json', 'r')
    content = f.read()
    f.close()
    chainid_data = json.loads(content)

    chainid_dict={}
    # 获取简化版的 id 与name
    for chainid_data_dict in chainid_data:
        chainid_dict[chainid_data_dict["chainId"]]=chainid_data_dict["name"]
    print(chainid_dict)

    for i in range(0,20):
        url=f"https://microservices.socket.tech/loki/tx-history?pageSize=50&page={i}"
        headers = {
                'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'
            }
        response = requests.get(url=url, headers=headers)
        print(response)
        print(i)
        # 获取列表数据
        list_data=response.json()["result"]

        # 设置保留的关键词
        # str_list=["_id","destTransactionHash","srcTransactionHash","fromChainId","toChainId","recipient","sender"]
        # 遍历列表数据，进行清洗 暂时不清洗
        for dict_data in list_data :
            # # 数据清洗，保留需要的关键词
            # dic_filter = {k: v for k, v in dict_data.items() if k in str_list}

            # 筛选有源链目标链哈希数据
            if "destTransactionHash" in dict_data.keys() and "srcTransactionHash" in dict_data.keys():
                # 将ID替换成链名
                fromChainId=dict_data["fromChainId"]
                toChainId=dict_data["toChainId"]
                if fromChainId in chainid_dict.keys() and toChainId in chainid_dict.keys():
                    dict_data["fromchainName"]=chainid_dict[fromChainId]
                    dict_data["tochainName"]=chainid_dict[toChainId]

                    # 统计链名
                    if chainid_dict[fromChainId] not in chain_name:
                        chain_name.append(chainid_dict[fromChainId])
                    if chainid_dict[toChainId] not in chain_name:
                        chain_name.append(chainid_dict[toChainId])

                    data.append(dict_data)

    # 将数据存放起来
    print(f"爬取有效数据总数量：{len(data)}")
    print(f"涉及的单链有{chain_name}")
    json_data = json.dumps(data, indent=4)
    with open('../data_new/multi_chain_data.json', 'w', encoding='utf-8') as f:
        f.write(json_data)

    return data


